/*
 *
 *  * Copyright (c) 2016. For Intelligent Group.
 *
 */

package com.intelligent.ispc.client.core.modal;

import com.google.common.collect.Lists;
import com.intelligent.ispc.core.dto.FileDataDto;

import javax.swing.table.AbstractTableModel;
import java.util.List;

/**
 * Created by tony.yu on 2016/8/26.
 * Update by cherry on 2016/09/26
 */
public class SearchCsvTableModel extends AbstractTableModel {
    private String[] columns;
    private List<FileDataDto> data;
    private List<FileDataDto> selectedDataCache;

    /**
     * Constructor
     */
    public SearchCsvTableModel() {
    }

    /**
     * Search the csv table model
     *
     * @param colums the table's listed
     */
    public SearchCsvTableModel(String[] colums) {
        this.columns = colums;
        data = Lists.newArrayList();
        selectedDataCache = Lists.newArrayList();
    }

    @Override
    public int getRowCount() {
        if (data == null) {
            return 0;
        } else {
            return data.size();
        }
    }

    @Override
    public int getColumnCount() {
        return 2;
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if (data.isEmpty()) {
            return new Object();
        }
        FileDataDto fileDataDto = data.get(rowIndex);
        String name = fileDataDto.getName();
        Boolean bool = fileDataDto.isCheck();
        if (columnIndex == 0) {
            return bool;
        } else if (columnIndex == 1) {
            return name;
        }

        return "";
    }

    @Override
    public void setValueAt(Object obj, int rowIndex, int columnIndex) {
        FileDataDto fileDataDto = data.get(rowIndex);
        if (columnIndex == 0) {
            fileDataDto.setCheck((Boolean) obj);
        } else if (columnIndex == 1) {
            fileDataDto.setName((String) obj);
        }
    }

    @Override
    public String getColumnName(int column) {
        return columns[column];
    }

    @Override
    public Class<?> getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }

    /**
     * Set CSV data
     *
     * @param dtos csv file datas
     */
    public void setData(List<FileDataDto> dtos) {
        if (dtos == null) {
            this.data = Lists.newArrayList();
        } else {
            this.data = dtos;
        }

        fireTableDataChanged();
    }

    /**
     * Set GRR data
     *
     * @param dtos csv file datas
     */
    public void setGrrData(List<FileDataDto> dtos) {
        if (dtos == null) {
            this.data = Lists.newArrayList();
        } else {
            this.data.clear();
            this.data.addAll(dtos);
            for (int i = 0; i < data.size(); i++) {
                if (data.get(i).isCheck()) {
                    setSelectOneCheck(i);
                    break;
                }
            }
        }
        fireTableDataChanged();
    }

    /**
     * Set check conditions
     *
     * @param aValue   true or false
     * @param rowIndex which line
     */
    public void setCheckValue(boolean aValue, int rowIndex) {
        data.get(rowIndex).setCheck(aValue);
        fireTableDataChanged();
    }

    public List<FileDataDto> getData() {
        return data;
    }

    /**
     * Get the line
     *
     * @param row which line
     * @return the data of lines
     */
    public FileDataDto getDataAtIndex(int row) {
        return data.get(row);
    }

    /**
     * Get the choose paths
     *
     * @return the paths list
     */
    public List<String> getCheckedPaths() {
        List<String> dtos = Lists.newArrayList();

        for (FileDataDto dto : data) {
            if (dto.isCheck()) {
                dtos.add(dto.getSavePath());
            }
        }
        return dtos;
    }

    /**
     * Set the selected line check or not
     *
     * @param rowIndex which line
     */
    public void setSelectOneCheck(int rowIndex) {
        if (data == null || data.size() <= rowIndex) {
            return;
        }
        for (int i = 0; i < data.size(); i++) {
            data.get(i).setCheck(false);
        }
        data.get(rowIndex).setCheck(true);
        fireTableDataChanged();
    }

    /**
     * Get the selected file
     *
     * @return the file list
     */
    public List<FileDataDto> getSelectedFile() {

        List<FileDataDto> fileDataDtos = Lists.newArrayList();

        for (FileDataDto dto : data) {
            if (dto.isCheck()) {
                fileDataDtos.add(dto);
            }
        }
        return fileDataDtos;
    }

    public List<FileDataDto> getSelectedDataCache() {
        return selectedDataCache;
    }

    /**
     * Set the cache of the selected data
     *
     * @param selectedDataCache selectedDataCache
     */
    public void setSelectedDataCache(List<FileDataDto> selectedDataCache) {
        this.selectedDataCache.clear();
        this.selectedDataCache.addAll(selectedDataCache);
    }

    /**
     * Check the selected data is valid whether or not
     * @return true when the selected is valid
     */
    public boolean isValidCatchData() {
        boolean result = true;
        List<String> checkedName = Lists.newArrayList();
        List<String> cacheName = Lists.newArrayList();
        if (selectedDataCache != null && selectedDataCache.isEmpty()) {
            return true;
        }
        for (FileDataDto dto : data) {
            if (dto.isCheck()) {
                checkedName.add(dto.getName());
            }
        }
        for (FileDataDto dto : selectedDataCache) {
            cacheName.add(dto.getName());
        }
        if (checkedName.size() != cacheName.size()) {
            return false;
        }
        for (String name : cacheName) {
            if (!checkedName.contains(name)) {
                result = false;
                break;
            }
        }
        return result;
    }
}
